/**
 * ...
 *
 */
(function ($) {
	$.fn.togglableFormFields = function () {
		return this.each(function () {
			var form = $(this).addClass('jquery-togglable-form-fields');
			var labels = form.find('label[for]');

			labels.each(function () {
				var label = $(this).addClass('jquery-togglable-form-fields');
				var input = form.find('#' + label.attr('for')).addClass('jquery-togglable-form-fields');

				label.toggle(function () {
					label.addClass('jquery-togglable-form-fields-open');
					input.addClass('jquery-togglable-form-fields-open');

					// If all are open
					if (!labels.filter(':not(.jquery-togglable-form-fields-open)').length) {
						form.addClass('jquery-togglable-form-fields-all-open');
					}

					setTimeout(function () {
						input.focus();
					}, 1000);
				}, function () {
					label.removeClass('jquery-togglable-form-fields-open');
					input.removeClass('jquery-togglable-form-fields-open');
					form.removeClass('jquery-togglable-form-fields-all-open');
				});
			});
		});
	};
})(jQuery);
